package org.yiqixue.accounts.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.yiqixue.accounts.dto.TransactionDto;
import org.yiqixue.accounts.service.ITransactionService;

import java.util.Date;
import java.util.List;

@Tag(
        name = "Transaction REST APIs for Accounts in CCBANK 新新银行",
        description = "REST APIs to fetch and insert transaction details"
)
@RestController
@RequestMapping(path = "/api/transactions", produces = {MediaType.APPLICATION_JSON_VALUE})
@AllArgsConstructor
public class TransactionController {

    private ITransactionService iTransactionService;

    /**
     * 根据手机号和交易类型查询当日的所有交易
     * @param mobileNumber 手机号
     * @param type 交易类型
     * @param transactionDate 交易日期
     * @return 符合条件的交易记录列表
     */
    @Operation(
            summary = "Fetch Transactions by Mobile Number and Type",
            description = "REST API to fetch all transactions of a certain type on a specific day for a given mobile number"
    )
    @ApiResponses({
            @ApiResponse(
                    responseCode = "200",
                    description = "HTTP Status OK"
            ),
            @ApiResponse(
                    responseCode = "500",
                    description = "HTTP Status Internal Server Error",
                    content = @Content(
                            schema = @Schema(implementation = String.class)
                    )
            )
    })
    @GetMapping
    public ResponseEntity<List<TransactionDto>> getTransactionsByMobileNumberAndType(
            @RequestParam String mobileNumber,
            @RequestParam String type,
            @RequestParam Date transactionDate) {
        var transactions = iTransactionService.getTransactionsByMobileNumberAndType(mobileNumber, type, transactionDate);
        return ResponseEntity.status(HttpStatus.OK).body(transactions);
    }

    /**
     * 插入一条交易记录
     * @param transactionDto 交易信息
     * @return HTTP 状态码
     */
    @Operation(
            summary = "Insert a Transaction",
            description = "REST API to insert a new transaction"
    )
    @ApiResponses({
            @ApiResponse(
                    responseCode = "201",
                    description = "HTTP Status Created"
            ),
            @ApiResponse(
                    responseCode = "500",
                    description = "HTTP Status Internal Server Error",
                    content = @Content(
                            schema = @Schema(implementation = String.class)
                    )
            )
    })
    @PostMapping
    public ResponseEntity<Void> insertTransaction(@Valid @RequestBody TransactionDto transactionDto) {
        iTransactionService.insertTransaction(transactionDto);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}